package com.rxjava.simpleflatmap;

/**
 * Created by iceGeneral on 2016/10/15.
 */
public final class OperatorMap<T, R> implements Operator<R, T> {

    final Func1<? super T, ? extends R> transformer;

    public OperatorMap(Func1<? super T, ? extends R> transformer) {
    	//transformer -> return Observable.from(new String[]{"flatMap" + integer, "flatMap" + 7, "flatMap" + 8});
        this.transformer = transformer;
    }

    @Override
    public Subscriber<? super T> call(final Subscriber<? super R> o) {
    	System.out.println("step5");
    	//o -> MergeSubscriber对象
        return new MapSubscriber<T, R>(o, transformer);
    }

    private class MapSubscriber<T, R> extends Subscriber<T> {

        private Subscriber<? super R> actual;
        private Func1<? super T, ? extends R> transformer;

        public MapSubscriber(Subscriber<? super R> o, Func1<? super T, ? extends R> transformer) {
        	//actual -> MergeSubscriber对象
            this.actual = o;
            //transformer -> return Observable.from(new String[]{"flatMap" + integer, "flatMap" + 7, "flatMap" + 8});
            this.transformer = transformer;
        }

        @Override
        public void onNext(T t) {
        	System.out.println("t -> " + t);//t=6
            R r = transformer.call(t);
            System.out.println("r -> " + r);
            actual.onNext(r);
        }
    }

}
